package com.beiding.workordersystem.orders.looporder.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.beiding.workordersystem.common.service.BaseService;
import com.beiding.workordersystem.common.standard.E;
import com.beiding.workordersystem.common.utils.ApplicationUtils;
import com.beiding.workordersystem.common.utils.QueryUtils;
import com.beiding.workordersystem.orders.looporder.dao.LoopTaskDao;
import com.beiding.workordersystem.orders.looporder.entity.LoopTask;
import com.beiding.workordersystem.orders.looporder.service.LoopTaskService;
import com.gitee.beiding.timer.MyTimer;
import com.gitee.beiding.timer.annotation.AspectHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

@Service
public class LoopTaskServiceImpl extends BaseService implements LoopTaskService {

    @Autowired
    private LoopTaskDao loopTaskDao;

    @Override
    public IPage page(IPage toIPage, String searchKey) {

        LambdaQueryWrapper<LoopTask> query = QueryUtils.getQuery(LoopTask.class);

        if (searchKey != null) {
            searchKey = searchKey.trim();
            if (notEmpty(searchKey)) {
                query.like(LoopTask::getCompany, searchKey).or().like(LoopTask::getLinkman, searchKey).or().like(LoopTask::getType, searchKey);
            }
        }

        return loopTaskDao.selectPage(toIPage, query);
    }

    @Override
    public void save(LoopTask copy) {
        if (fillId(copy)) {
            copy.setStatus(1);
            loopTaskDao.insert(copy);
        } else {
            loopTaskDao.updateById(copy);
        }

    }

    @Override
    @Transactional
    public void delete(String[] ids) {
        for (String id : ids) {
            loopTaskDao.deleteById(id);
        }
    }

    @Autowired
    private MyTimer myTimer;


    //TODO  任务结束的前一个月向前台推送提醒
    @Override
    public void start(String[] ids) {
        for (String id : ids) {
            LoopTask loopTask = loopTaskDao.selectById(id);
            E.notNull(loopTask, "任务不存在");
            E.isTrue(loopTask.getStatus() == 1, "状态不满足预期");
            loopTask.setStatus(2);
            loopTaskDao.updateById(loopTask);

            //到期后处理相关逻辑
            myTimer.schedule("expireLoopTask", id, loopTask.getEndTime());
        }
    }

    @Override
    public void postpone(String id, Integer moon) {
        LoopTask loopTask = loopTaskDao.selectById(id);
        E.notNull(loopTask, "任务不存在");
        E.isTrue(loopTask.getStatus().equals(3), "非法操作");
        GregorianCalendar gc = new GregorianCalendar();

        //从当前时间推
        gc.setTime(new Date());
        gc.add(Calendar.MONTH, moon);
        loopTask.setEndTime(gc.getTime());
        loopTask.setStatus(2);
        loopTaskDao.updateById(loopTask);

        ApplicationUtils.get(this).expireLoopTask(id, loopTask.getEndTime());

    }

    @AspectHandler
    public void expireLoopTask(String id, Date time) {
        LoopTask loopTask = loopTaskDao.selectById(id);
        if (loopTask != null) {
            if (loopTask.getStatus().equals(2) && System.currentTimeMillis() >= loopTask.getEndTime().getTime()) {
                loopTask.setStatus(3);
                loopTaskDao.updateById(loopTask);
            }
        }
    }

}
